<?php
require_once 'init.php';
require_once 'Zend/Search/Lucene.php';
require_once 'Zend/Search/Lucene/Document.php';
require_once 'Zend/Search/Lucene/Field.php';

class index_data {

	private static $_config = null; //config
	private static $_doc = null; // doc 
	private static $_db	= null; // db connection
	private static $_index = null;
	
	public function __construct() {
		self::$_config = Zend_Registry::get('config');
		self::$_db		= Zend_Registry::get('db');
		self::$_index = new Zend_Search_Lucene(self::$_config['index']['data_dir'], true);
		self::$_doc	= new Zend_Search_Lucene_Document();
		$this->getInfo();
	}
	
	public function index_data() {
		$this->__construct();
	}
	
	private function run() {
		
	}
	
	public function getInfo() {
		foreach(self::$_config['index']['table'] as $key=>$value) {
			$list = implode(',', $value);
			$keyarr = array_keys($value);
			$keylist = implode(',', $keyarr);
			$sql='SElECT '.$keylist. ' FROM '.$key.' LIMIT 1';
			$fromDb = $this->getInfoFromDb($sql);
			//var_dump($fromDb);
			$listCount = count($value);
			foreach($fromDb as $k => $val) {
				foreach($val as $ke => $var) {
					if(self::$_config['db']['charset']!='UTF8') {
						iconv(self::$_config['db']['charset'], 'UTF-8', $var);
					}
					var_dump($var);
					self::$_doc->addField(Zend_Search_Lucene_Field::Keyword($k,$var));
					self::$_index->addDocument(self::$_doc);
					self::$_index->commit();
				}
			}
			
		}
	}
	
	public function getInfoFromDb($sql) {
		return $res = self::$_db->getAll($sql);
	}
}

$index = new index_data();